var gotxinh = window.location.origin;
var numberFormat = "#.##0";
var jNumberFormat = {format: numberFormat};
$(document).ready(init);
function init(){
            if($("#main-slider").length>0){
                $("#main-slider").carouFredSel({
                    items               : 1,
                    direction           : "left",
                    pagination          : ".pag",
                    scroll              : {
                        items           : 1,
                        easing          : "swing",
                        duration        : 1000,                        
                        pauseOnHover    : true
                    }
                });
            }
            jQuery('input.o_vuong').each(function() {
               jQuery(this).prettyCheckable(); 
            });
             $( "#slider-range" ).slider({
                range: true,
                min: 0,
                max: 50,
                values: [ 5, 50 ],
                slide: function( event, ui ) {
                    $( "#amountmin" ).val( $( "#slider-range" ).slider( "values", 0 ) + ".000.000đ");
                    $( "#amountmax" ).val( $( "#slider-range" ).slider( "values", 1 ) + ".000.000đ");
                }
            });
            $( "#amountmin" ).val( $( "#slider-range" ).slider( "values", 0 ) + ".000đ");
            $( "#amountmax" ).val( $( "#slider-range" ).slider( "values", 1 ) + ".000đ");

    if($("#sidebar").length >0){
        var wrap = $("#sidebar");
        var arrId = [];
        var arrColor = [];
        var arrBrand = [];
        var arrSize = [];
        $.each($(wrap).find('.box'),function(index, row){
            $.each($(row).find('.list'),function(j,k){
                $(k).find('li div').click(function(){
                    if($(k).attr('id') == 'subsubcate'){
                        var id = $(this).parent().attr('rel');
                        if($(this).hasClass('ck')) {
                            arrId.push(id);
                        }else{
                            arrId.splice($.inArray(id, arrId),1);
                        }
                    }
                    if($(k).attr('id') == 'color'){
                        var id = $(this).parent().attr('rel');
                        if($(this).hasClass('ck')) {
                            arrColor.push(id);
                        }else{
                            arrColor.splice($.inArray(id, arrColor),1);
                        }
                    }
                    if($(k).attr('id') == 'brand'){
                        var id = $(this).parent().attr('rel');
                        if($(this).hasClass('ck')) {
                            arrBrand.push(id);
                        }else{
                            arrBrand.splice($.inArray(id, arrBrand),1);
                        }
                    }
                    if($(k).attr('id') == 'size'){
                        var id = $(this).parent().attr('rel');
                        if($(this).hasClass('ck')) {
                            arrSize.push(id);
                        }else{
                            arrSize.splice($.inArray(id, arrSize),1);
                        }
                    }
                    if(arrSize.length >0 || arrColor.length >0 || arrBrand.length>0 || arrId.length>0)
                        search_product(arrId,arrColor,arrBrand,arrSize);
                });
            });
        });
        
        $("#orderDisplay").change(function(){
            search_product(arrId,arrColor,arrBrand,arrSize);
        });
        $("#showProduct").change(function(){
           search_product(arrId,arrColor,arrBrand,arrSize); 
        });
        $("#slider-range").find('a').each(function(index,row){
            $(this).click(function(){
                search_product(arrId,arrColor,arrBrand,arrSize);     
            })
            
            
        })
    }
}
function search_product(subsubcate,color,brand,size){
    var arrSubSub = '';
    var arrColor = '';
    var arrBrand = '';
    var arrSize = '';
    var showProduct = $("#showProduct").val();
    var orderDisplay = $("#orderDisplay").val();
    // var min = $("#amountmin" ).val().replace("đ","");
    // var max = $("#amountmax" ).val().replace("đ","");
    for(var i = 0;i<subsubcate.length;i++){
        arrSubSub += subsubcate[i];
        if(i+1 < subsubcate.length){
            arrSubSub += ',';
        }else{
            arrSubSub +='';
        }
    }
    for(var i = 0;i<color.length;i++){
        arrColor += color[i];
        if(i+1 < color.length){
            arrColor += ',';
        }else{
            arrColor +='';
        }
    }
    for(var i = 0;i<brand.length;i++){
        arrBrand += brand[i];
        if(i+1 < brand.length){
            arrBrand += ',';
        }else{
            arrBrand +='';
        }
    }
    for(var i = 0;i<size.length;i++){
        arrSize += size[i];
        if(i+1 < size.length){
            arrSize += ',';
        }else{
            arrSize +='';
        }
    }
    var url = gotxinh + "/product/search_product";
    var urls = window.location.pathname;
    var ids = urls.substring(urls.lastIndexOf('/') + 1).replace(".html","");
    //'minprice':min,'maxprice':max,
    $.ajax({
        url: url,
        type:'post',
        data: {'showProduct':showProduct,'orderDisplay':orderDisplay,'id' : arrSubSub,'color' : arrColor, 'brand' : arrBrand, 'size': arrSize,'subid' : ids},
        dataType:'json',
        beforeSend:function(){
            $("#loading").show();
            $("#product").empty();
        },
        success:resultProduct,
        error:function(){
            
        }

    });
}
function resultProduct(data){
    $("#loading").hide();
    var html = '';
    //$.formatNumber(value['Price'], jNumberFormat)
    if(data[0].length >0){
        var i =0;
        $.each(data[0], function(key,value){
            if(i%3 == 0){
                html += '<li class="first">';
                html += '<img src="'+gotxinh+'/timthumb.php?src=/images/cover/' + value['CoverPhoto'] +'&amp;h=200&amp;w=216" width="216" height="200"><br>';
                html += '<a href="'+gotxinh+'/product/' + value['ProductNameRewrite'] +'">' + value['ProductName'] + '</a><br/>';
                html += '<p class="price">' + value['Price'] + 'đ</p>';
                html += '</li>';
            }else{
                html += '<li>';
                html += '<img src="'+gotxinh+'/timthumb.php?src=/images/cover/' + value['CoverPhoto'] +'&amp;h=200&amp;w=216" width="216" height="200"><br>';
                html += '<a href="'+gotxinh+'/product/' + value['ProductNameRewrite'] +'">' + value['ProductName'] + '</a><br/>';
                html += '<p class="price">' + value['Price'] + 'đ</p>';
                html += '</li>';
            }
            i++;
        });
    }else{
        html = "Không có sản phẩm nào được tìm thấy";
    }
        if($("#product").length > 0){
            $("#product").html(html);
        }
}